package com.ming.enums;

import lombok.Getter;

/**
 * 统一错误码枚举
 *
 * @author ming
 * @version 1.0
 */
@Getter
public enum ErrorCode {

    // ========== 通用错误码 ==========
    SUCCESS(200, "成功"),

    // 客户端错误 4xx
    BAD_REQUEST(400, "请求参数错误"),
    UNAUTHORIZED(401, "未授权，请登录"),
    FORBIDDEN(403, "禁止访问，权限不足"),
    NOT_FOUND(404, "资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方法不支持"),
    REQUEST_TIMEOUT(408, "请求超时"),
    TOO_MANY_REQUESTS(429, "请求过于频繁，请稍后再试"),

    // 服务器错误 5xx
    INTERNAL_SERVER_ERROR(500, "系统内部异常"),
    BAD_GATEWAY(502, "网关错误"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),
    GATEWAY_TIMEOUT(504, "网关超时"),

    // ========== 业务错误码 5xxx ==========
    BUSINESS_ERROR(5000, "业务异常"),
    DATA_NOT_FOUND(5001, "数据不存在"),
    DATA_DUPLICATE(5002, "数据已存在"),
    DATA_INVALID(5003, "数据无效"),
    OPERATION_NOT_ALLOWED(5004, "操作不允许"),
    RESOURCE_BUSY(5005, "资源正在被使用，请稍后再试"),

    // ========== 用户相关错误码 5100-5199 ==========
    USER_NOT_FOUND(5101, "用户不存在"),
    USER_DISABLED(5102, "用户已被禁用"),
    USER_LOCKED(5103, "用户已被锁定"),
    PASSWORD_INCORRECT(5104, "密码错误"),
    PASSWORD_EXPIRED(5105, "密码已过期"),
    TOKEN_INVALID(5106, "令牌无效"),
    TOKEN_EXPIRED(5107, "令牌已过期"),
    TOKEN_MISSING(5108, "令牌缺失"),
    LOGIN_FAILED(5109, "登录失败"),
    LOGOUT_FAILED(5110, "登出失败"),

    // ========== 部门相关错误码 5200-5299 ==========
    DEPT_NOT_FOUND(5201, "部门不存在"),
    DEPT_HAS_EMPLOYEES(5202, "部门下存在员工，无法删除"),
    DEPT_NAME_EXISTS(5203, "部门名称已存在"),
    DEPT_CODE_EXISTS(5204, "部门编码已存在"),
    DEPT_LEVEL_INVALID(5205, "部门层级无效"),

    // ========== 员工相关错误码 5300-5399 ==========
    EMP_NOT_FOUND(5301, "员工不存在"),
    EMP_USERNAME_EXISTS(5302, "用户名已存在"),
    EMP_EMAIL_EXISTS(5303, "邮箱已存在"),
    EMP_PHONE_EXISTS(5304, "手机号已存在"),
    EMP_ID_CARD_EXISTS(5305, "身份证号已存在"),

    // ========== 文件相关错误码 5400-5499 ==========
    FILE_NOT_FOUND(5401, "文件不存在"),
    FILE_UPLOAD_FAILED(5402, "文件上传失败"),
    FILE_TYPE_NOT_SUPPORTED(5403, "不支持的文件类型"),
    FILE_SIZE_EXCEEDED(5404, "文件大小超出限制"),
    FILE_DELETE_FAILED(5405, "文件删除失败"),
    FILE_DOWNLOAD_FAILED(5406, "文件下载失败"),

    // ========== 数据库错误码 6000-6099 ==========
    DATABASE_ERROR(6000, "数据库异常"),
    DATABASE_CONNECTION_ERROR(6001, "数据库连接异常"),
    DATA_INTEGRITY_VIOLATION(6002, "数据完整性约束违反"),
    DATA_ACCESS_ERROR(6003, "数据访问异常"),

    // ========== 第三方服务错误码 6100-6199 ==========
    THIRD_PARTY_SERVICE_ERROR(6100, "第三方服务异常"),
    OSS_SERVICE_ERROR(6101, "对象存储服务异常"),
    SMS_SERVICE_ERROR(6102, "短信服务异常"),
    EMAIL_SERVICE_ERROR(6103, "邮件服务异常"),

    // ========== 系统配置错误码 6200-6299 ==========
    CONFIG_ERROR(6200, "系统配置异常"),
    CONFIG_MISSING(6201, "配置项缺失"),
    CONFIG_INVALID(6202, "配置项无效");

    /**
     * 错误码
     */
    private final int code;

    /**
     * 错误信息
     */
    private final String message;

    ErrorCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 根据错误码获取枚举
     */
    public static ErrorCode getByCode(int code) {
        for (ErrorCode errorCode : values()) {
            if (errorCode.getCode() == code) {
                return errorCode;
            }
        }
        return INTERNAL_SERVER_ERROR;
    }

    /**
     * 判断是否为客户端错误
     */
    public boolean isClientError() {
        return this.code >= 400 && this.code < 500;
    }

    /**
     * 判断是否为服务器错误
     */
    public boolean isServerError() {
        return this.code >= 500;
    }

    /**
     * 判断是否为业务错误
     */
    public boolean isBusinessError() {
        return this.code >= 5000;
    }
}